//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using Tathata.Common.Caching;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using System.Globalization;
using Tathata.Common.DAL;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.EntityCollections;
using Tathata.Common.DAL.Managers;

namespace Tathata.Common.BLL.Managers
{
    /// <summary>
    /// Tax category manager
    /// </summary>
    public class TaxCategoryManager
    {
        #region Constants
        private const string TAXCATEGORIES_ALL_KEY = "Nop.taxcategory.all";
        private const string TAXCATEGORIES_BY_ID_KEY = "Nop.taxcategory.id-{0}";
        #endregion

        #region Methods
        /// <summary>
        /// Deletes a tax category
        /// </summary>
        /// <param name="TaxCategoryID">The tax category identifier</param>
        public static void DeleteTaxCategory(int TaxCategoryID)
        {
            TaxCategoryDBManager.DeleteTaxCategory(TaxCategoryID);
            if (NopContext.IsCachable)
            {
                NopCache.RemoveByPattern(TAXCATEGORIES_ALL_KEY);
                NopCache.RemoveByPattern(TAXCATEGORIES_BY_ID_KEY);
            }
        }

        /// <summary>
        /// Gets all tax categories
        /// </summary>
        /// <returns>Tax category collection</returns>
        public static TaxCategoryCollection GetAllTaxCategories()
        {
            string key = string.Format(TAXCATEGORIES_ALL_KEY);
            object obj2 = NopCache.Get(key);
            if (NopContext.IsCachable && (obj2 != null))
            {
                return (TaxCategoryCollection)obj2;
            }

            TaxCategoryCollection taxCategoryCollection = TaxCategoryDBManager.GetAllTaxCategories();
            if (NopContext.IsCachable)
            {
                NopCache.Max(key, taxCategoryCollection);
            }
            return taxCategoryCollection;
        }

        /// <summary>
        /// Gets a tax category
        /// </summary>
        /// <param name="TaxCategoryID">Tax category identifier</param>
        /// <returns>Tax category</returns>
        public static TaxCategory GetByTaxCategoryID(int TaxCategoryID)
        {
            string key = string.Format(TAXCATEGORIES_BY_ID_KEY, TaxCategoryID);
            object obj2 = NopCache.Get(key);
            if (NopContext.IsCachable && (obj2 != null))
            {
                return (TaxCategory)obj2;
            }

            TaxCategory taxCategory = TaxCategoryDBManager.GetByTaxCategoryID(TaxCategoryID);

            if (NopContext.IsCachable)
            {
                NopCache.Max(key, taxCategory);
            }
            return taxCategory;
        }

        /// <summary>
        /// Inserts a tax category
        /// </summary>
        /// <param name="Name">The name</param>
        /// <param name="DisplayOrder">The display order</param>
        /// <param name="CreatedOn">The date and time of instance creation</param>
        /// <param name="UpdatedOn">The date and time of instance update</param>
        /// <returns>Tax category</returns>
        public static TaxCategory InsertTaxCategory(string Name,
            int DisplayOrder, DateTime CreatedOn, DateTime UpdatedOn)
        {
            TaxCategory taxCategory = TaxCategoryDBManager.InsertTaxCategory(Name,
             DisplayOrder, CreatedOn, UpdatedOn);
            if (NopContext.IsCachable)
            {
                NopCache.RemoveByPattern(TAXCATEGORIES_ALL_KEY);
                NopCache.RemoveByPattern(TAXCATEGORIES_BY_ID_KEY);
            }
            return taxCategory;
        }

        /// <summary>
        /// Updates the tax category
        /// </summary>
        /// <param name="TaxCategoryID">The tax category identifier</param>
        /// <param name="Name">The name</param>
        /// <param name="DisplayOrder">The display order</param>
        /// <param name="CreatedOn">The date and time of instance creation</param>
        /// <param name="UpdatedOn">The date and time of instance update</param>
        /// <returns>Tax category</returns>
        public static TaxCategory UpdateTaxCategory(int TaxCategoryID, string Name,
            int DisplayOrder, DateTime CreatedOn, DateTime UpdatedOn)
        {
            TaxCategory taxCategory = TaxCategoryDBManager.UpdateTaxCategory(TaxCategoryID, Name,
             DisplayOrder, CreatedOn, UpdatedOn);
            if (NopContext.IsCachable)
            {
                NopCache.RemoveByPattern(TAXCATEGORIES_ALL_KEY);
                NopCache.RemoveByPattern(TAXCATEGORIES_BY_ID_KEY);
            }
            return taxCategory;
        }
        #endregion
    }
}
